package cn.edu.stu.syllabus.main;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import javax.inject.Inject;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.switcher.DayNightSwitcher;
import cn.edu.stu.syllabus.utils.ActivityUtils;
import cn.edu.stu.syllabus.utils.GuideHelper;
import cn.edu.stu.syllabus.utils.GuideSaver;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, Toolbar.OnMenuItemClickListener {

    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.nav_view)
    NavigationView mNavView;

    @Inject
    MainPresenter mPresenter;

    AlertDialog mGuideOpenDialog;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        setTitle("汕大课程表");
        mToolbar.inflateMenu(R.menu.menu_main);
        mToolbar.setOnMenuItemClickListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        if (mNavView != null) {
            mNavView.setNavigationItemSelectedListener(this);
        }

        MainFragment mainFragment = (MainFragment) getSupportFragmentManager()
                .findFragmentById(R.id.contentFrame);
        if (mainFragment == null) {
            mainFragment = MainFragment.newInstance();

            ActivityUtils.addFragmentToActivity(getSupportFragmentManager()
                    , mainFragment, R.id.contentFrame);
        }

        DaggerMainComponent.builder()
                .mainPresenterModule(new MainPresenterModule(mainFragment))
                .build().inject(this);

        if (GuideSaver.getInstance().isFirstOpenApp()) {
            mGuideOpenDialog = new AlertDialog.Builder(this)
                    .setTitle("关闭使用向导")
                    .setMessage("目前已经启动使用向导,是否关闭?\n(建议首次使用该版本时不进行关闭)")
                    .setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            closeGuide();
                        }
                    })
                    .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            openAndStartGuide();
                        }
                    })
                    .setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            openAndStartGuide();

                        }
                    })
                    .setCancelable(false)
                    .create();
            mGuideOpenDialog.show();
        } else {
            startGuide();
        }

    }

    private void openAndStartGuide() {
        GuideSaver.getInstance().setGuideOpen(true);
        GuideSaver.getInstance().markHaveOpenedApp();
        startGuide();
    }

    private void closeGuide() {
        GuideSaver.getInstance().setGuideOpen(false);
        GuideSaver.getInstance().markHaveOpenedApp();
    }

    private void startGuide() {
        GuideHelper.startActSeqNow(this,
                GuideHelper.getNavTapTarget(mToolbar,
                        "这是抽屉导航",
                        "打开后，你可以进行一系列的操作")
                        .id(1)
                ,
                GuideHelper.getToolbarItemTapTarget(mToolbar, R.id.action_dayNight,
                        "这是切换(日/夜)间模式的按钮",
                        "点击后, 你可以切换为(日/夜)间模式")
                        .id(2)
        );
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        mDrawerLayout.closeDrawer(GravityCompat.START);

        return true;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int itemID = item.getItemId();
        if (itemID == R.id.action_dayNight) {
            DayNightSwitcher.getInstance().switchActMode(MainActivity.this);
        }
        return true;
    }
}
